/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 * Created on 18.4.2011, 10:41:36
 */

package src.clovece;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import src.clovece.game.GameInfo;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import src.clovece.game.Save;

/**
 *  Class serving as menu with choices for new game
 */
public class NewSingleplayerDialog extends javax.swing.JDialog {
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();
    private GameFrame parent = null;
    private JLabel label1 = null;
    private JLabel label2 = null;
    private JLabel label3 = null;
    private JLabel label4 = null;

    public NewSingleplayerDialog(GameFrame parent) {
        super(parent,true);
        Point p = parent.getLocation();
        this.parent= parent;
        this.setLocation(p.x +200, p.y +100);
    }

    /*
     * Initialises and shows dialog
     */
    public void showYourself() {
        this.initComponents();

        ButtonGroup group = new ButtonGroup();
        group.add(this.RadioButton_player1);
        group.add(this.RadioButton_player2);
        group.add(this.RadioButton_player3);
        group.add(this.RadioButton_player4);

        if(this.gameInfo.getChosenFigurine() == 0) this.RadioButton_player1.setSelected(true);
        else if(this.gameInfo.getChosenFigurine() == 1) this.RadioButton_player2.setSelected(true);
        else if(this.gameInfo.getChosenFigurine() == 2) this.RadioButton_player3.setSelected(true);
        else if(this.gameInfo.getChosenFigurine() == 3) this.RadioButton_player4.setSelected(true);

        if(this.gameInfo.getGameBoardSize() == 36) this.GameBoardSizeComboBox.setSelectedIndex(0);
        else if(this.gameInfo.getGameBoardSize() == 48) this.GameBoardSizeComboBox.setSelectedIndex(1);
        if(this.gameInfo.getGameBoardSize() == 60) this.GameBoardSizeComboBox.setSelectedIndex(2);

        if(this.gameInfo.getNumberOfPlayers() == 2) this.NumberOfPlayersComboBox.setSelectedIndex(0);
        else if(this.gameInfo.getNumberOfPlayers() == 3) this.NumberOfPlayersComboBox.setSelectedIndex(1);
        else if(this.gameInfo.getNumberOfPlayers() == 4) this.NumberOfPlayersComboBox.setSelectedIndex(2);

        this.NicknameTextField.setText(this.gameInfo.getPlayerNickname(0));
        
        this.label1 = new JLabel();
        this.label2 = new JLabel();
        this.label3 = new JLabel();
        this.label4 = new JLabel();


        this.label1.setIcon(Figurine(this.gameInfo.getGraphicMode(), 0));
        this.label1.setBounds(50,60,40,40);

        this.label2.setIcon(Figurine(this.gameInfo.getGraphicMode(), 1));
        this.label2.setBounds(50,110,40,40);

        this.label3.setIcon(Figurine(this.gameInfo.getGraphicMode(), 2));
        this.label3.setBounds(140,60,40,40);

        this.label4.setIcon(Figurine(this.gameInfo.getGraphicMode(), 3));
        this.label4.setBounds(140,110,40,40);

        this.PlayerPanel.add(label1);
        this.PlayerPanel.add(label2);
        this.PlayerPanel.add(label3);
        this.PlayerPanel.add(label4);

        this.GraphicModeComboBox.setSelectedIndex(this.gameInfo.getGraphicMode());

        this.setVisible(true);

    }


    /*
     * Loads image of a figurine
     * @param int graphic set
     * @param int figurine
     */
    private ImageIcon Figurine(int gameSet, int figurine)
    {
        ImageIcon icon = new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                  + "lib" + System.getProperty("file.separator") + "img"
                  + System.getProperty("file.separator") + "figurine" + gameSet + figurine + "a" + ".png");
        return icon;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        StartGameButton = new javax.swing.JToggleButton();
        GameTab = new javax.swing.JPanel();
        GameBoardSizeLabel = new javax.swing.JLabel();
        NumberOfPlayersComboBox = new javax.swing.JComboBox();
        NumberOfPlayersLabel = new javax.swing.JLabel();
        GameBoardSizeComboBox = new javax.swing.JComboBox();
        NicknameTextField = new javax.swing.JTextField();
        NicknameLabel = new javax.swing.JLabel();
        PlayerPanel = new javax.swing.JPanel();
        GraphicModeComboBox = new javax.swing.JComboBox();
        GraphicModeLabel = new javax.swing.JLabel();
        RadioButton_player4 = new javax.swing.JRadioButton();
        RadioButton_player3 = new javax.swing.JRadioButton();
        RadioButton_player1 = new javax.swing.JRadioButton();
        RadioButton_player2 = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setResizable(false);

        StartGameButton.setText("Start Game");
        StartGameButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                StartGameButtonMouseClicked(evt);
            }
        });

        GameTab.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Game", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));

        GameBoardSizeLabel.setText("Game board size");

        NumberOfPlayersComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "3", "4" }));

        NumberOfPlayersLabel.setText("Number of players");

        GameBoardSizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "small", "medium", "large" }));

        NicknameTextField.setColumns(10);

        NicknameLabel.setText("Nickname:");

        javax.swing.GroupLayout GameTabLayout = new javax.swing.GroupLayout(GameTab);
        GameTab.setLayout(GameTabLayout);
        GameTabLayout.setHorizontalGroup(
            GameTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(GameTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(GameTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(GameTabLayout.createSequentialGroup()
                        .addGroup(GameTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(GameBoardSizeLabel)
                            .addComponent(NumberOfPlayersLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(GameTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(NumberOfPlayersComboBox, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(GameBoardSizeComboBox, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(GameTabLayout.createSequentialGroup()
                        .addComponent(NicknameLabel)
                        .addGap(18, 18, 18)
                        .addComponent(NicknameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        GameTabLayout.setVerticalGroup(
            GameTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(GameTabLayout.createSequentialGroup()
                .addGroup(GameTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(GameBoardSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(GameBoardSizeLabel))
                .addGap(18, 18, 18)
                .addGroup(GameTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NumberOfPlayersLabel)
                    .addComponent(NumberOfPlayersComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(GameTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NicknameLabel)
                    .addComponent(NicknameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        PlayerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Graphics", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));

        GraphicModeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mode 1", "Mode 2" }));
        GraphicModeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GraphicModeComboBoxActionPerformed(evt);
            }
        });

        GraphicModeLabel.setText("Graphic Mode:");

        RadioButton_player4.setMnemonic(KeyEvent.VK_D);

        RadioButton_player3.setMnemonic(KeyEvent.VK_D);

        RadioButton_player1.setMnemonic(KeyEvent.VK_D);
        RadioButton_player1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RadioButton_player1ActionPerformed(evt);
            }
        });

        RadioButton_player2.setMnemonic(KeyEvent.VK_D);

        javax.swing.GroupLayout PlayerPanelLayout = new javax.swing.GroupLayout(PlayerPanel);
        PlayerPanel.setLayout(PlayerPanelLayout);
        PlayerPanelLayout.setHorizontalGroup(
            PlayerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PlayerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PlayerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadioButton_player1)
                    .addComponent(GraphicModeLabel)
                    .addComponent(RadioButton_player2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(PlayerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadioButton_player3)
                    .addComponent(GraphicModeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RadioButton_player4))
                .addGap(166, 166, 166))
        );
        PlayerPanelLayout.setVerticalGroup(
            PlayerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PlayerPanelLayout.createSequentialGroup()
                .addGroup(PlayerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(GraphicModeLabel)
                    .addComponent(GraphicModeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PlayerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadioButton_player3)
                    .addComponent(RadioButton_player1))
                .addGap(28, 28, 28)
                .addGroup(PlayerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadioButton_player4)
                    .addComponent(RadioButton_player2))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(GameTab, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PlayerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(191, 191, 191)
                        .addComponent(StartGameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(PlayerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(GameTab, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(StartGameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void StartGameButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_StartGameButtonMouseClicked
        this.setVisible(false);

        // set new single game
        this.gameInfo.setNewGame(true);

        // game board size
        if(this.GameBoardSizeComboBox.getSelectedItem().toString().equals("small")) {
            this.gameInfo.setGameBoardSize(36);
        }
        else if(this.GameBoardSizeComboBox.getSelectedItem().toString().equals("medium")) {
            this.gameInfo.setGameBoardSize(48);
        }
        else if(this.GameBoardSizeComboBox.getSelectedItem().toString().equals("large")) {
            this.gameInfo.setGameBoardSize(60);
        }

        // number of players
        this.gameInfo.setNumberOfPlayers(Integer.parseInt(this.NumberOfPlayersComboBox.getSelectedItem().toString()));

        // singleplayer / multiplater
        this.gameInfo.setLocalGame(true);

        // player name
        if(!this.NicknameTextField.getText().equals("")) {
            this.gameInfo.setPlayerNickname(this.NicknameTextField.getText(),0);
        }

        // graphic mode
        if(this.GraphicModeComboBox.getSelectedItem().toString().equals("Mode 1")) {
            this.gameInfo.setGraphicMode(0);
        }
        else {
            this.gameInfo.setGraphicMode(1);
        }

        // chosen figurine
        if(this.RadioButton_player1.isSelected()) {
            this.gameInfo.setChosenFigurine(0);
        }
        else if(this.RadioButton_player2.isSelected()) {
            this.gameInfo.setChosenFigurine(1);
        }
        else if(this.RadioButton_player3.isSelected()) {
            this.gameInfo.setChosenFigurine(2);
        }
        else {
            this.gameInfo.setChosenFigurine(3);
        }

        //TODO doplnit ulozenie grafickych nastaveni
        this.parent.fillGameFrame();
        Save save = new Save();
        this.dispose();
    }//GEN-LAST:event_StartGameButtonMouseClicked

    private void GraphicModeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GraphicModeComboBoxActionPerformed
        if(this.GraphicModeComboBox.getSelectedItem().toString().equals("Mode 1")) {
            this.setGraphicMode1();
        }
        else {
            this.setGraphicMode2();
        }
    }//GEN-LAST:event_GraphicModeComboBoxActionPerformed

    private void RadioButton_player1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RadioButton_player1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_RadioButton_player1ActionPerformed

    /**
     * Loads figurines from first graphic set
     */
    private void setGraphicMode1() {
        this.label1.setIcon(this.Figurine(0, 0));
        this.label2.setIcon(this.Figurine(0, 1));
        this.label3.setIcon(this.Figurine(0, 2));
        this.label4.setIcon(this.Figurine(0, 3));
    }

    /**
     * Loads figurines from second graphic set
     */
    private void setGraphicMode2() {
        this.label1.setIcon(this.Figurine(1, 0));
        this.label2.setIcon(this.Figurine(1, 1));
        this.label3.setIcon(this.Figurine(1, 2));
        this.label4.setIcon(this.Figurine(1, 3));
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox GameBoardSizeComboBox;
    private javax.swing.JLabel GameBoardSizeLabel;
    private javax.swing.JPanel GameTab;
    private javax.swing.JComboBox GraphicModeComboBox;
    private javax.swing.JLabel GraphicModeLabel;
    private javax.swing.JLabel NicknameLabel;
    private javax.swing.JTextField NicknameTextField;
    private javax.swing.JComboBox NumberOfPlayersComboBox;
    private javax.swing.JLabel NumberOfPlayersLabel;
    private javax.swing.JPanel PlayerPanel;
    private javax.swing.JRadioButton RadioButton_player1;
    private javax.swing.JRadioButton RadioButton_player2;
    private javax.swing.JRadioButton RadioButton_player3;
    private javax.swing.JRadioButton RadioButton_player4;
    private javax.swing.JToggleButton StartGameButton;
    // End of variables declaration//GEN-END:variables

}
